package com.stockz.watchlist.dao;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.stockz.equity.util.JpaUtil;
import com.stockz.watchlist.model.Entry;
import com.stockz.watchlist.model.EntryPrimaryKey;
import com.stockz.watchlist.model.EntrySearchCriteria;

@Repository
public class EntryDaoImpl implements EntryDao {

	@Autowired
	private JpaUtil jpaUtil;
	
	public void persist(Entry entry) {
		jpaUtil.getEntityManager().persist(entry);
	}

	public Entry merge(Entry entry) {
		return jpaUtil.getEntityManager().merge(entry);
	}

	public void remove(Entry entry) {
		jpaUtil.getEntityManager().remove(entry);
	}

	public Entry get(EntryPrimaryKey entryPrimaryKey) {
		return jpaUtil.getEntityManager().find(Entry.class, entryPrimaryKey);
	}

	@SuppressWarnings("unchecked")
	public List<Entry> getByWatchListId(Long id) {
		Query query = jpaUtil.getEntityManager().createQuery("select e from Entry e where e.watchList.id = :id", Entry.class);
		query.setParameter("id", id);
		return query.getResultList();
	}

	public List<Entry> find(EntrySearchCriteria entry) {
		CriteriaBuilder builder = jpaUtil.getEntityManager().getCriteriaBuilder();
		CriteriaQuery<Entry> criteria = builder.createQuery(Entry.class);
		Root<Entry> root = criteria.from(Entry.class);
		
		
		
		return null;
	}

}
